package com.studorm.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.studorm.Connection_mysql;
import com.studorm.vo.Kuaijian;

public class KuaiJianAction  extends ActionSupport {
			
	/**
	 * 
	 */
	private static final long serialVersionUID = -3697921271744988026L;
	private List<Kuaijian> tableRow = new ArrayList<Kuaijian>();
	private int amountOfRow = 0;
	public List<Kuaijian> getTableRow() {
		return tableRow;
	}
	public void setTableRow(List<Kuaijian> tableRow) {
		this.tableRow = tableRow;
	}
	
	public String loadExpress() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String dormBdName = request.getParameter("dornBdName"); 
		String sign = request.getParameter("issignin");
		String rowEachP_t = request.getParameter("rowEachP");//每页行数
		String nuOfP_t = request.getParameter("nuOfP");//要获取第几页的数据
		int issignin;
		
		if(sign.equals("已领取"))
			{
			issignin=1;
			}
		else 
			{
			issignin=0;
			}
		
		int rowEachP = 10; //每页行数
		int nuOfP = 1;//当前是第几页
		if(rowEachP_t != null){
			rowEachP = Integer.parseInt(rowEachP_t);
		}
		if(nuOfP_t != null){
			nuOfP = Integer.parseInt(nuOfP_t);
		}
		
		Connection conn = Connection_mysql.getConnection();
		PreparedStatement ps;
		ResultSet res;
		String sql="SELECT COUNT(*) FROM studormcms.expressmanage WHERE dormBdName=? AND issignin=? ";
		ps = conn.prepareStatement(sql);
		ps.setString(1,dormBdName);
		ps.setInt(2,issignin);
		res = ps.executeQuery();
		while(res.next()){
			amountOfRow = res.getInt(1);
		}
		res.close();
		ps.close();
		
		sql="SELECT dormBdName,dormNu,studentNu,name,arrivalTime,issignin,id,signforTime FROM studormcms.expressmanage WHERE dormBdName=? AND issignin=? LIMIT ?, ?";
		ps = conn.prepareStatement(sql);
		
		ps.setString(1,dormBdName);
		ps.setInt(2,issignin);
		ps.setInt(3, (nuOfP-1) * rowEachP);
		ps.setInt(4, rowEachP);
		
		res = ps.executeQuery();	
		while(res.next()){
			Kuaijian express = new Kuaijian();
			express.setDormBdName(res.getString("dormBdName"));	
			express.setDormNum(res.getString("dormNu"));
			express.setStuNum(res.getString("studentNu"));
			express.setStuName(res.getString("name"));
			express.setArrivalTime(res.getTimestamp("arrivalTime"));
			express.setIssignin(res.getInt("issignin"));
			express.setSignforTime(res.getTimestamp("signforTime"));
			express.setId(res.getInt("id"));
			tableRow.add(express);
		}
		
		res.close();
		ps.close();
		conn.close();
		
		return SUCCESS;
	}
	
	public String delExpress() throws Exception{
		//读取请求的参数 
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String expressId = request.getParameter("delId");
		int id = 0;
		if(expressId != null){
			id = Integer.parseInt(expressId);
		}else{
			return NONE;
		}
		Connection conn = Connection_mysql.getConnection();
		String sql = "DELETE FROM express WHERE id = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		int res = ps.executeUpdate();

		ps.close();
		conn.close();
		if(res == 1){
			return SUCCESS;
		}else{
			return NONE;
		}
	}
	
	public String alterSign() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String id = request.getParameter("alterId");
		String status = request.getParameter("status");
		
		//获取当前系统时间
		java.util.Date time = new java.util.Date();
		java.sql.Date signfortime = new java.sql.Date(time.getTime());
		
		//规范时间日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String datetime = sdf.format(signfortime); 
		
		
		if(id == null || id.isEmpty()){
			return NONE;
		}
		String sql = "UPDATE express SET issignin = ?,signforTime = ? WHERE id=?";
		Connection conn = Connection_mysql.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(3, Integer.parseInt(id));
		if(status.equals("未领取")){
			ps.setInt(1, 1);
			ps.setString(2,datetime);
		}else if(status.equals("已领取")){
			ps.setInt(1, 0);
			datetime=null;
			ps.setString(2,datetime);
		}else
		{
			return NONE;
		}
		
		int res = ps.executeUpdate();
		ps.close();
		conn.close();
		if(res >= 1){
			return SUCCESS;
		}else{
			return NONE;
		}
	}
	
public String addExpress() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String stuID = request.getParameter("stuID");
		String sign = request.getParameter("sign");
		String arrival = request.getParameter("arrivaltime");
		String signtime = request.getParameter("signtime");
		
		int issign;
		if(sign.equals("未领取"))
		{
			issign=0;
		}
		else {
			issign=1;
		}

		//Pattern patternNu = Pattern.compile("\\d{3}");// 匹配3位整数
		//Pattern patternDeci = Pattern.compile("\\d*\\.{0,1}\\d*"); // 匹配小数或整数
		//Pattern patternYM = Pattern.compile("[1-2]\\d*-[0-1]{0,1}[0-9]");// 匹配 ????-??的年月格式
		
		if(stuID.isEmpty()){
			return NONE;
		}
		if(sign == null || sign.isEmpty() ){
			return NONE;
		}
		if(arrival == null || arrival.isEmpty()){
			return NONE;
		}
		
		if(signtime.isEmpty())
		{
			signtime = null;
		}

		Connection conn = Connection_mysql.getConnection();
		String sql = "INSERT INTO express (recipients,arrivalTime,signforTime,issignin) VALUES (?,?,?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, stuID);
		ps.setString(2, arrival);
		ps.setString(3, signtime);
		ps.setInt(4,issign);

		int res = ps.executeUpdate();

		ps.close();
		conn.close();
		if(res < 1){
			return NONE;
		}else{
			return SUCCESS;
		}
	}
	
public String editExpress() throws Exception{
	
	HttpServletRequest request = ServletActionContext.getRequest(); 
	String stuID = request.getParameter("stuID");
	String sign = request.getParameter("sign");
	String signtime = request.getParameter("signtime");
	String arrivaltime = request.getParameter("arrivaltime");
	String id = request.getParameter("Expid");
	int issign;
	if(sign.equals("未领取"))
	{
		issign=0;
	}
	else {
		issign=1;
	}

	
	Pattern patternNu = Pattern.compile("\\d*");
	//Pattern patternDeci = Pattern.compile("\\d*\\.{0,1}\\d*"); // 匹配小数或整数
	//Pattern patternYM = Pattern.compile("[1-2]\\d*-[0-1]{0,1}[0-9]");// 匹配 ????-??的年月格式
	
	if(id == null || id.isEmpty() || !patternNu.matcher(id).matches()){
		return NONE;
	}

	if(arrivaltime == null || arrivaltime.isEmpty()){
		return NONE;
	}
	if(sign == null || sign.isEmpty()){
		return NONE;
	}
	
	if(signtime.isEmpty())
	{
		signtime = null;
	}

	
	
	Connection conn = Connection_mysql.getConnection();
	String sql = "UPDATE express SET recipients=?,issignin=?,arrivalTime=?,signforTime=? WHERE id = ?";
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setInt(5, Integer.parseInt(id));
	ps.setString(1, stuID);
	ps.setInt(2,issign);
	ps.setString(3,arrivaltime);
	ps.setString(4,signtime);
	  // change at 11.24 bug

	int res = ps.executeUpdate();

	ps.close();
	conn.close();
	if(res < 1){
		return NONE;
	}else{
		return SUCCESS;
	}
}


public String loadAExpress() throws Exception{
	HttpServletRequest request = ServletActionContext.getRequest(); 
	String ExpId = request.getParameter("editId");
	Pattern patternNu = Pattern.compile("\\d*");
	if(ExpId == null || ExpId.isEmpty() || !patternNu.matcher(ExpId).matches()){
		return NONE;
	}
	String sql = "SELECT id, studentNu,arrivalTime,signforTime,issignin FROM studormcms.expressmanage WHERE id = ?";
	Connection conn = Connection_mysql.getConnection();
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setInt(1, Integer.parseInt(ExpId));
	ResultSet res = ps.executeQuery();
	while(res.next()){
		Kuaijian express = new Kuaijian();
		express.setStuNum(res.getString("studentNu"));
		express.setArrivalTime(res.getTimestamp("arrivalTime"));
		express.setSignforTime(res.getTimestamp("signforTime"));
		express.setIssignin(res.getInt("issignin"));
		express.setId(res.getInt("id"));
		
		
		tableRow.add(express);
	}
	res.close();
	ps.close();
	conn.close();
	
	return SUCCESS;
}


public String loadStuExpress() throws Exception
{
	HttpServletRequest request = ServletActionContext.getRequest(); 
	HttpSession session = request.getSession();
	String id = (String)session.getAttribute("id");
	String sign = request.getParameter("issignin");	
	int issign;
	if(id == null ||id.isEmpty()){		
		return NONE;
	}
	if(sign.equals("未领取"))
	{
		issign = 0;
	}
	else {
		issign=1;
	}
	String sql = "SELECT  name,studentNu,arrivalTime,signforTime,issignin FROM studormcms.expressmanage WHERE studentNu = ? and issignin=?";
	Connection conn = Connection_mysql.getConnection();
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setInt(1, Integer.parseInt(id));
	ps.setInt(2,issign);
	ResultSet res = ps.executeQuery();
	while(res.next()){
		Kuaijian express = new Kuaijian();
		express.setStuNum(res.getString("studentNu"));
		express.setArrivalTime(res.getTimestamp("arrivalTime"));
		express.setSignforTime(res.getTimestamp("signforTime"));
		express.setIssignin(res.getInt("issignin"));
		express.setStuName(res.getString("name"));	
		tableRow.add(express);
	}
	res.close();
	ps.close();
	conn.close();
	
	return SUCCESS;
}



	public int getAmountOfRow() {
		return amountOfRow;
	}
	public void setAmountOfRow(int amountOfRow) {
		this.amountOfRow = amountOfRow;
	}
}
